package com.pjc.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.mongodb.util.JSON;
import com.pjc.db.MongoDB;
import com.pjc.db.MongoUtils;
import com.pjc.model.User;
import com.pjc.utils.Utils;

/**
 * 用户数据访问层类
 * 
 * @author 王畅
 * @since 2015年6月13日
 */
public class UserDao {

	protected static final Logger LOG = LoggerFactory.getLogger(UserDao.class);

	/**
	 * 获取user表对象
	 * 
	 * @author 王畅
	 * @return
	 * @throws Exception
	 */
	private static DBCollection getUserCollection() throws Exception {
		return MongoDB.getDB("jkt").getCollection("User");
	}

	/**
	 * 获取tablePrimaryKey表对象
	 * 
	 * @author 王畅
	 * @return
	 * @throws Exception
	 */
	private static DBCollection getPrimaryKeyCollection() throws Exception {
		return MongoDB.getDB("jkt").getCollection("tablePrimaryKey");
	}

	/**
	 * 获取流水号的值
	 * 
	 * @author 王畅
	 * @return
	 * @throws Exception
	 */
	public static Long generateRecordID() throws Exception {
		DBObject query = new BasicDBObject();
		query.put("_id", "USER_PRIMARY_KEY");
		DBObject update = new BasicDBObject();
		update.put("$inc", new BasicDBObject("recordID", Integer.valueOf(1)));
		DBObject dbObject = MongoUtils.findAndModify(getPrimaryKeyCollection(), query, update);
		if (dbObject == null) {
			DBObject ref = new BasicDBObject();
			ref.put("_id", "USER_PRIMARY_KEY");
			ref.put("recordID", Long.valueOf(101L));
			MongoUtils.save(getPrimaryKeyCollection(), ref);
			return Long.valueOf(101L);
		}
		return (Long) dbObject.get("recordID");
	}

	/**
	 * 根据用户UID与appID获取单个用户对象
	 * 
	 * @param userID
	 * @param appID
	 * @return
	 * @throws Exception
	 */
	public static User getUser(String userID, String appID) throws Exception {
		DBObject ref = new BasicDBObject();
		ref.put("userId", userID);
		ref.put("appId", appID);
		DBObject object = MongoUtils.findOne(getUserCollection(), ref);

		if (object == null) {
			return null;
		}
		return (User) Utils.JSON2Object(object.toString(), User.class);

	}

	/**
	 * 保存用户对象
	 * 
	 * @param scrUser
	 * @return
	 * @throws Exception
	 */
	public static boolean saveUser(User scrUser) throws Exception {
		WriteResult writeResult = MongoUtils.save(getUserCollection(), (DBObject) JSON.parse(Utils.object2JSON(scrUser)));
		return writeResult.getError() == null;
	}

	/**
	 * 修改用户名密码
	 * 
	 * @author 王畅
	 * @param userID
	 * @param pwd
	 * @param appID
	 * @return
	 * @throws Exception
	 */
	public static boolean updateUserPwd(String userID, String pwd, String appID) throws Exception {
		DBObject query = new BasicDBObject();
		query.put("userId", userID);
		query.put("appId", appID);
		DBObject val = new BasicDBObject();
		val.put("pwd", pwd);
		DBObject update = new BasicDBObject("$set", val);
		WriteResult writeResult = MongoUtils.update(getUserCollection(), query, update);
		if (null != writeResult) {
			if (writeResult.getN() > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 设置用户的下线时间
	 * 
	 * @author 王畅
	 * @param userId
	 * @param time
	 * @param appID
	 * @return
	 * @throws Exception
	 */
	public static boolean updateLogoutTime(String userId, long time, String appID) throws Exception {
		DBObject key = new BasicDBObject("userId", userId);
		key.put("appId", appID);
		DBObject field = new BasicDBObject("$set", new BasicDBObject("logoutTime", Long.valueOf(time)));
		WriteResult writeResult = MongoUtils.update(getUserCollection(), key, field);
		if (null != writeResult) {
			if (writeResult.getN() > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 用户上线的状态
	 * 
	 * @param userId
	 * @param online
	 * @param onlineTime
	 * @param appID
	 * @return
	 * @throws Exception
	 */
	public static boolean updateUserOnlineStatus(String userId, int online, long onlineTime, String appId) throws Exception {
		DBObject key = new BasicDBObject("userId", userId);
		key.put("appId", appId);
		DBObject val = new BasicDBObject();
		val.put("$set", new BasicDBObject("onLineState", online));
		val.put("$inc", new BasicDBObject("onLineTime", Long.valueOf(onlineTime / 1000L)));
		WriteResult writeResult = MongoUtils.update(getUserCollection(), key, val);
		if (null != writeResult) {
			if (writeResult.getN() > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 更新登录状态
	 * 
	 * @author 王畅
	 * @param userID
	 * @param version
	 * @param loginTime
	 * @param appID
	 * @return
	 * @throws Exception
	 */
	public static boolean updateLoginState(String userID, String version, long loginTime, String appID) throws Exception {
		DBObject query = new BasicDBObject("userId", userID);
		query.put("appId", appID);
		DBObject ref = new BasicDBObject();
		ref.put("version", version);
		ref.put("onLineState", Integer.valueOf(1));
		ref.put("loginTime", Long.valueOf(loginTime));
		DBObject update = new BasicDBObject();
		update.put("$set", ref);
		update.put("$inc", new BasicDBObject("loginCount", Integer.valueOf(1)));
		WriteResult writeResult = MongoUtils.update(getUserCollection(), query, update);
		if (null != writeResult) {
			if (writeResult.getN() > 0) {
				return true;
			}
		}
		return false;
	}

}
